<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" width="1000" top="100px" destroy-on-close
		@closed="$emit('closed')">
		<el-form :model="formData" style="width:500px" label-width="80px" label-position="right">
			<el-form-item label="岗位名称">
				<el-input v-model="formData.name" clearable placeholder="请填写岗位名称"></el-input>
			</el-form-item>
			<el-form-item label="职责说明">
				<el-input v-model="formData.decs" type="textarea" clearable placeholder="请填写职责说明"></el-input>
			</el-form-item>
			<el-form-item label="权限">
				<el-tree ref="tree" :data="data" show-checkbox node-key="id" :default-expanded-keys="[2, 3]"
					:default-checked-keys="[5]" :props="defaultProps" @check="getCurrentNode" />
			</el-form-item>
			<el-form-item label="状态" prop="boolean">
				<el-radio-group v-model="formData.name">
					<el-radio label="正常">正常</el-radio>
					<el-radio label="禁用">禁用</el-radio>
				</el-radio-group>
			</el-form-item>
			<div class="u-text-center">
				<el-button type="primary">保存</el-button>
			</div>
		</el-form>
		<template #footer>
			<el-button @click="visible=false">返 回</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
	export default {
		emits: ['success', 'closed'],
		data() {
			return {
				showAMap: false,
				mode: "add",
				titleMap: {
					add: '新增',
					edit: '编辑',
					show: '查看'
				},
				visible: false,
				isSaveing: false,
				//表单数据
				formData: {
					name: ''
				},
				//验证规则
				rules: {
					name: [{
						required: true,
						message: '请输入姓名'
					}]
				},
				//所需数据选项
				groups: [],
				groupsProps: {
					value: "id",
					emitPath: false,
					checkStrictly: true
				},
				data: [{
					id: 1,
					label: '一级 2',
					children: [{
						id: 3,
						label: '二级 2-1',
						children: [{
							id: 4,
							label: '三级 3-1-1'
						}, {
							id: 5,
							label: '三级 3-1-2',
							//disabled: true
						}]
					}, {
						id: 2,
						label: '二级 2-2',
						//disabled: true,
						children: [{
							id: 6,
							label: '三级 3-2-1'
						}, {
							id: 7,
							label: '三级 3-2-2',
							//disabled: true
						}]
					}]
				}],
			}
		},
		watch: {
			'form.address'(nVal) {
				if (!nVal) {
					this.showAMap = false
				} else {
					this.showAMap = true
				}
			}
		},
		mounted() {

		},
		methods: {
			getCurrentNode(nodeObj, nodeState) {
				//判断当前状态是选中还是取消选中
				//const isCheck = this.$refs.tree.getCheckedNodes().indexOf(nodeObj) > -1
				console.log(nodeObj,nodeState)
			},

			comfirmBmap(mapData) {
				console.log(mapData)
				this.form.lng = mapData.lng
				this.form.lat = mapData.lat
			},
			//显示
			open(mode = 'add') {
				this.mode = mode;
				this.visible = true;
				return this
			},
			//表单提交方法
			async submit() {
				var valid = await this.$refs.dialogForm.validate().catch(() => {})
				if (!valid) {
					return false
				}
				this.isSaveing = true;
				var res = await this.$API.demo.post.post(this.form);
				this.isSaveing = false;
				if (res.code == 200) {
					if (this.mode == 'add') {
						this.form.id = res.data
					}
					this.$emit('success', this.form, this.mode)
					this.visible = false;
					this.$message.success("操作成功")
				} else {
					this.$alert(res.message, "提示", {
						type: 'error'
					})
				}
			},
			//表单注入数据
			setData(data) {
				// this.form.id = data.id
				// this.form.name = data.name
				// this.form.sex = data.sex
				// this.form.email = data.email
				// this.form.num = data.num
				// this.form.boolean = data.boolean

				//可以和上面一样单个注入，也可以像下面一样直接合并进去
				Object.assign(this.formData, data)
			}
		}
	}
</script>

<style>
</style>
